<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>Spring方法注入的使用与实现原理 | 博客主页 | 世界是个球，前方总有路！</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="Spring">
    <meta name="description" content="一、前言&amp;emsp;&amp;emsp;这几天为了更详细地了解Spring，我开始阅读Spring的官方文档。说实话，之前很少阅读官方文档，就算是读，也是读别人翻译好的。但是最近由于准备春招，需要了解很多知识点的细节，网上几乎搜索不到，只能硬着头皮去读官方文档。虽然我读的这个Spring文档也是中文版的，但是很明显是机翻，十分不通顺，只能对着英文版本，两边对照着看，这个过程很慢，也很吃力。但是这应该是一个">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring方法注入的使用与实现原理">
<meta property="og:url" content="http://tewuyiang.gitee.io/blog/Spring%E6%96%B9%E6%B3%95%E6%B3%A8%E5%85%A5%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/index.html">
<meta property="og:site_name" content="博客主页">
<meta property="og:description" content="一、前言&amp;emsp;&amp;emsp;这几天为了更详细地了解Spring，我开始阅读Spring的官方文档。说实话，之前很少阅读官方文档，就算是读，也是读别人翻译好的。但是最近由于准备春招，需要了解很多知识点的细节，网上几乎搜索不到，只能硬着头皮去读官方文档。虽然我读的这个Spring文档也是中文版的，但是很明显是机翻，十分不通顺，只能对着英文版本，两边对照着看，这个过程很慢，也很吃力。但是这应该是一个">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-05-12T15:43:13.000Z">
<meta property="article:modified_time" content="2020-05-12T18:25:01.697Z">
<meta property="article:author" content="特务依昂">
<meta property="article:tag" content="Spring">
<meta name="twitter:card" content="summary">
    
        <link rel="alternate" type="application/atom+xml" title="博客主页" href="/blog/atom.xml">
    
    <link rel="shortcut icon" href="/blog/img/title.png">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/blog/img/brand.jpg)">
      <div class="brand">
        <a href="/blog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">特务依昂</h5>
          <a href="mailto:1131564805@qq.com" title="1131564805@qq.com" class="mail">1131564805@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/blog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                博客
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/tewuyiang" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://weibo.com/u/5516635708/" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.cnblogs.com/tuyang1129/" target="_blank" >
                <i class="icon icon-lg icon-link"></i>
                博客园
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Spring方法注入的使用与实现原理</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Spring方法注入的使用与实现原理</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-05-12T15:43:13.000Z" itemprop="datePublished" class="page-time">
  2020-05-12
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/Spring/">Spring</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#一、前言"><span class="post-toc-number">1.</span> <span class="post-toc-text">一、前言</span></a></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#二、正文"><span class="post-toc-number">2.</span> <span class="post-toc-text">二、正文</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#2-1-问题分析"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">2.1 问题分析</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#2-2-方法注入的功能"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">2.2 方法注入的功能</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#2-3-方法注入的实现原理"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">2.3 方法注入的实现原理</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#2-4-查找方法注入的使用"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">2.4 查找方法注入的使用</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#三、总结"><span class="post-toc-number">3.</span> <span class="post-toc-text">三、总结</span></a></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#四、参考"><span class="post-toc-number">4.</span> <span class="post-toc-text">四、参考</span></a></li></ol>
        </nav>
    </aside>


<article id="post-Spring方法注入的使用与实现原理"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Spring方法注入的使用与实现原理</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-05-12 23:43:13" datetime="2020-05-12T15:43:13.000Z"  itemprop="datePublished">2020-05-12</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/Spring/">Spring</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h1 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h1><p>&emsp;&emsp;这几天为了更详细地了解<code>Spring</code>，我开始阅读<code>Spring</code>的官方文档。说实话，之前很少阅读官方文档，就算是读，也是读别人翻译好的。但是最近由于准备春招，需要了解很多知识点的细节，网上几乎搜索不到，只能硬着头皮去读官方文档。虽然我读的这个<code>Spring</code>文档也是中文版的，但是很明显是机翻，十分不通顺，只能对着英文版本，两边对照着看，这个过程很慢，也很吃力。但是这应该是一个程序员必须要经历的过程吧。</p>
<p>&emsp;&emsp;在读文档的时候，我读到了一个叫做方法注入的内容，这是我之前学习<code>Spring</code>所没有了解过的。所以，这篇博客就参照文档中的描述，来讲一讲这个方法注入是什么，在什么情况下使用，以及简单谈一谈它的实现原理。</p>
<br>

<h1 id="二、正文"><a href="#二、正文" class="headerlink" title="二、正文"></a>二、正文</h1><h2 id="2-1-问题分析"><a href="#2-1-问题分析" class="headerlink" title="2.1 问题分析"></a>2.1 问题分析</h2><p>&emsp;&emsp;在说方法注入之前，我们先来考虑一种实际情况，通过实际案例，来引出我们为什么需要方法注入。在我们的<code>Spring</code>程序中，可以将<code>bean</code>的依赖关系简单分为四种：</p>
<ol>
<li>单例<code>bean</code>依赖单例<code>bean</code>；</li>
<li>多例<code>bean</code>依赖多例<code>bean</code>；</li>
<li>多例<code>bean</code>依赖单例<code>bean</code>；</li>
<li>单例<code>bean</code>依赖多例<code>bean</code>；</li>
</ol>
<p>&emsp;&emsp;前三种依赖关系都很好解决，<code>Spring</code>容器会帮我们正确地处理，唯独第四种——单例<code>bean</code>依赖多例<code>bean</code>，<code>Spring</code>容器无法帮我们得到想要的结果。为什么这么说呢？我们可以通过<code>Spring</code>容器工作的方式来分析。</p>
<p>&emsp;&emsp;我们知道，<code>Spring</code>中<code>bean</code>的作用域默认是单例的，每一个<code>Spring</code>容器，只会创建这个类型的一个实例对象，并缓存在容器中，所以对这个<code>bean</code>的请求，拿到的都是同一个<code>bean</code>实例。而对于每一个<code>bean</code>来说，容器只会为它进行一次依赖注入，那就是在创建这个<code>bean</code>，为它初始化的时候。于是我们可以开始考虑上面说的第四种依赖情况了。假设一个单例<code>bean A</code>，它依赖于多例<code>bean B</code>，<code>Spring</code>容器在创建<code>A</code>的时候，发现它依赖于<code>B</code>，且<code>B</code>是多例的，于是容器会创建一个新的<code>B</code>，然后将它注入到<code>A</code>中。<code>A</code>创建完成后，由于它是单例的，所以会被缓存在容器中。之后，所有访问<code>A</code>的代码，拿到的都是同一个<code>A</code>对象。而且，由于容器只会为<code>bean</code>执行一次依赖注入，所以我们通过<code>A</code>访问到的<code>B</code>，永远都是同一个，尽管<code>B</code>被配置为了多例，但是并没有用。为什么会这样？因为多例的含义是，我们每次向<code>Spring</code>容器请求多例<code>bean</code>，都会创建一个新的对象返回。而<code>B</code>虽然是多例，但是我们是通过<code>A</code>访问<code>B</code>，并不是通过容器访问，所以拿到的永远是同一个<code>B</code>。这时候，单例<code>bean</code>依赖多例<code>bean</code>就失败了。</p>
<p>&emsp;&emsp;那要如何解决这个问题呢？解决方案应该不难想到。我们可以放弃让<code>Spring</code>容器为我们注入<code>B</code>，而是编写一个方法，这个方法直接向<code>Spring</code>容器请求<code>B</code>；然后在<code>A</code>中，每次想要获取<code>B</code>时，就调用这个方法获取，这样每次获取到的<code>B</code>就是不一样的了。而且我们这里可以借助<code>ApplicationContextAware</code>接口，将<code>context</code>对象（也就是容器）存储在<code>A</code>中，这样就可以方便地调用<code>getBean</code>获取<code>B</code>了。比如，<code>A</code>的代码可以是这样：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> <span class="keyword">implements</span> <span class="title">ApplicationContextAware</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 记录容器的引用</span></span><br><span class="line">    <span class="keyword">private</span> ApplicationContext context;</span><br><span class="line">    <span class="comment">// A依赖的多例对象B</span></span><br><span class="line">    <span class="keyword">private</span> B b;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 这是一个回调方法，会在bean创建时被调用</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setApplicationContext</span><span class="params">(ApplicationContext applicationContext)</span></span></span><br><span class="line"><span class="function">            <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.context = applicationContext;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> B <span class="title">getB</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 每次获取B时，都向容器申请一个新的B</span></span><br><span class="line">        b = context.getBean(B<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">        <span class="keyword">return</span> b;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;但是，上面的做法真的好吗？答案显然是不好。<code>Spring</code>的一个很大的优点就是，它侵入性很低，我们在自己编写的代码中，几乎看不到<code>Spring</code>的组件，一般只会有一些注解。但是上面的代码中，却直接耦合了<code>Spring</code>容器，将容器存储在类中，并显式地调用了容器的方法，这不仅增加了<code>Spring</code>的侵入性，也让我们的代码变得不那么容易管理，也变得不再优雅。而<code>Spring</code>提供的<strong>方法注入</strong>机制，就是用了实现和上面类似的功能，但是更加地优雅，侵入性更低。下面我们就来看一看。</p>
<br>

<h2 id="2-2-方法注入的功能"><a href="#2-2-方法注入的功能" class="headerlink" title="2.2 方法注入的功能"></a>2.2 方法注入的功能</h2><p>&emsp;&emsp;什么是方法注入？其实方法注入和<code>AOP</code>非常类似，<code>AOP</code>用来对我们定义的方法进行增强，而<strong>方法注入，则是用来覆盖我们定义的方法</strong>。通过<code>Spring</code>提供的方法注入机制，我们可以对类中定义的方法进行替换，比如说上面的<code>getB</code>方法，正常情况下，它的实现应该是这样的：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> B <span class="title">getB</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> b;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;但是，为了实现每次获取<code>B</code>时，能够让<code>Spring</code>容器创建一个新的<code>B</code>，我们在上面的代码中将它修改成了下面这个样子：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> B <span class="title">getB</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 每次获取B时，都向容器申请一个新的B</span></span><br><span class="line">    b = context.getBean(B<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">    <span class="keyword">return</span> b;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;但是，我们之前也说过，这种方式并不好，因为这直接依赖于<code>Spring</code>容器，增加了耦合性。而方法注入可以帮助我们解决这一点。方法注入能帮我们完成上面的替换，而且这种替换是隐式地，由<code>Spring</code>容器自动帮我们替换。我们并不需要修改编写代码的方式，仍然可以将<code>getB</code>方法写成第一种形式，而<code>Spring</code>容器会自动帮我们替换成第二种形式。这样就可以在不增加耦合的情况下，实现我们的目的。</p>
<br>

<h2 id="2-3-方法注入的实现原理"><a href="#2-3-方法注入的实现原理" class="headerlink" title="2.3 方法注入的实现原理"></a>2.3 方法注入的实现原理</h2><p>&emsp;&emsp;那方法注入的实现原理是什么呢？我之前说过，方法注入和<code>AOP</code>类似，不仅仅是功能类似，实际上它们的实现方式也是一样的。<strong>方法注入的实现原理，就是通过CGLib的动态代理</strong>。关于<code>AOP</code>的实现原理，可以参考我的这篇博客：<a href="https://www.cnblogs.com/tuyang1129/p/12878549.html" target="_blank" rel="noopener">浅析Spring中AOP的实现原理——动态代理</a>。</p>
<p>&emsp;&emsp;如果我们为一个类的方法，配置了方法注入，那么在<code>Spring</code>容器创建这个类的对象时，实际上创建的是一个代理对象。<code>Spring</code>会使用<code>CGLib</code>操作这个类的字节码，生成类的一个子类，然后覆盖需要修改的那个方法，而在创建对象时，创建的就是这个子类（代理类）的对象。而具体覆盖成什么样子，取决于我们的配置。比如说<code>Spring</code>提供了一个具体的方法注入机制——<strong>查找方法注入</strong>，这种方法注入，可以将方法替换为一个查找方法，它的功能就是去<code>Spring</code>容器中获取一个特定的<code>Bean</code>，而获取哪一个<code>bean</code>，取决于方法的返回值以及我们指定的<code>bean</code>名称。</p>
<p>&emsp;&emsp;比如说，上面的<code>getB</code>方法，如果我们对它使用了查找方法注入，那么<code>Spring</code>容器会使用<code>CGLib</code>生成<code>A</code>类的一个子类（代理类），覆盖<code>A</code>类的<code>getB</code>方法，由于<code>getB</code>方法的返回值是<code>B</code>类型，于是这个方法的功能就变成了去<code>Spring</code>容器中获取一个<code>B</code>，当然，我们也可以通过<code>bean</code>的名称，指定这个方法查找的<code>bean</code>。下面我就通过实际代码，来演示查找方法注入。</p>
<br>

<h2 id="2-4-查找方法注入的使用"><a href="#2-4-查找方法注入的使用" class="headerlink" title="2.4 查找方法注入的使用"></a>2.4 查找方法注入的使用</h2><p><strong>（一）通过xml配置</strong></p>
<p>&emsp;&emsp;为了演示查找方法注入，我们需要几个具体的类，假设我们有两个类<code>User</code>和<code>Car</code>，而<code>User</code>依赖于<code>Car</code>，它们的定义如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line">    <span class="comment">// 依赖于car</span></span><br><span class="line">    <span class="keyword">private</span> Car car;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 为这个方法进行注入</span></span><br><span class="line">   	<span class="function"><span class="keyword">public</span> Car <span class="title">getCar</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> car;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">	<span class="comment">// 省略其他setter和getter，以及toString方法</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> speed;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span> price;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 省略setter和getter，以及toString方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;好，现在有了这两个类，我们可以开始进行方法注入了。我们模拟之前说过的依赖关系——单例<code>bean</code>依赖于多例<code>bean</code>，将<code>User</code>配置为单例，而将<code>User</code>依赖的<code>Car</code>配置为多例。则配置文件如下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 将user的作用域定义为singleton --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"user"</span> <span class="attr">class</span>=<span class="string">"cn.tewuyiang.pojo.User"</span> <span class="attr">scope</span>=<span class="string">"singleton"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"name"</span> <span class="attr">value</span>=<span class="string">"aaa"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"age"</span> <span class="attr">value</span>=<span class="string">"28"</span> /&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">        配置查找方法注入，替换getCar方法，让他成为从spring容器中查找car的一个工厂方法</span></span><br><span class="line"><span class="comment">        name指定了需要进行方法注入的方法，而bean则指定了这个方法被覆盖后，是用来查找哪个bean的</span></span><br><span class="line"><span class="comment">    --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">lookup-method</span> <span class="attr">name</span>=<span class="string">"getCar"</span> <span class="attr">bean</span>=<span class="string">"car"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 将car的作用域定义为prototype --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"car"</span> <span class="attr">class</span>=<span class="string">"cn.tewuyiang.pojo.Car"</span> <span class="attr">scope</span>=<span class="string">"prototype"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"price"</span> <span class="attr">value</span>=<span class="string">"9999.35"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"speed"</span> <span class="attr">value</span>=<span class="string">"100"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;好，到此为止，我们就配置完成了，下面就该测试一下通过<code>user</code>的<code>getCar</code>方法拿到的多个<code>car</code>，是不是不相同。如果方法注入没有生效，那么按理来讲，我们调用<code>getCar</code>方法返回的应该是<code>null</code>，因为我们并没有配置将car的值注入user中。但是如果方法注入生效，那么我们通过<code>getCar</code>，就可以拿到<code>car</code>对象，因为它将去<code>Spring</code>容器中获取，而且每次获取到的都不是同一个。测试方法如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testXML</span><span class="params">()</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">    <span class="comment">// 创建Spring容器</span></span><br><span class="line">    ClassPathXmlApplicationContext context =</span><br><span class="line">        <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="string">"classpath:applicationContext.xml"</span>);</span><br><span class="line">    <span class="comment">// 获取User对象</span></span><br><span class="line">    User user = context.getBean(User<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">    <span class="comment">// 多次调用getCar方法，获取多个car</span></span><br><span class="line">    Car c1 = user.getCar();</span><br><span class="line">    Car c2 = user.getCar();</span><br><span class="line">    Car c3 = user.getCar();</span><br><span class="line">    <span class="comment">// 分别输出car的hash值，看是否相等，以此判断是否是同一个对象</span></span><br><span class="line">    System.out.println(c1.hashCode());</span><br><span class="line">    System.out.println(c2.hashCode());</span><br><span class="line">    System.out.println(c3.hashCode());</span><br><span class="line">    <span class="comment">// 输出user这个bean所属类型的父类</span></span><br><span class="line">    System.out.println(user.getClass().getSuperclass());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;上面的测试逻辑应该很好理解，除了最后一句，为什么需要输出<code>user</code>这个<code>bean</code>所属类型的父类。因为我前面说过，方法注入通过<code>CGLib</code>动态代理实现，而<code>CGLib</code>动态代理的原理就是生成类的一个子类。我们为<code>User</code>类使用了方法注入，所以我们拿到的<code>user</code>这个<code>bean</code>，应该是一个代理<code>bean</code>，并且它的类型是<code>User</code>的子类。所以我们输出这个<code>bean</code>的父类，来判断是否和我们之前说的一样。输出结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1392906938</span><br><span class="line">708890004</span><br><span class="line">255944888</span><br><span class="line">class cn.tewuyiang.pojo.User	&#x2F;&#x2F; 父类果然是User</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;可以看到，我们果然能够通过<code>getCar</code>方法，获取到<code>bean</code>，并且每一次获取到的都不是同一个，因为<code>hashcode</code>不相等。同时，<code>user</code>这个<code>bean</code>的父类型果然是<code>User</code>，说明<code>user</code>这个<code>bean</code>确实是<code>CGLib</code>生成的一个代理<code>bean</code>。到此，也就证明了我们之前的叙述。</p>
<br>

<p><strong>（二）通过注解配置</strong></p>
<p>&emsp;&emsp;上面通过<code>xml</code>的配置方式，大致了解了查找方法注入的使用，下面我们再来看看使用注解，如何实现。其实使用注解的方式更加简单，我们只需要在方法上使用<code>@Lookup</code>注解即可，<code>User</code>和<code>Car</code>的配置如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line">    <span class="keyword">private</span> Car car;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 使用Lookup注解，告诉Spring这个方法需要使用查找方法注入</span></span><br><span class="line">    <span class="comment">// 这里直接使用@Lookup，则Spring将会依据方法返回值</span></span><br><span class="line">    <span class="comment">// 将它覆盖为一个在Spring容器中获取Car这个类型的bean的方法</span></span><br><span class="line">    <span class="comment">// 但是也可以指定需要获取的bean的名字，如：@Lookup("car")</span></span><br><span class="line">    <span class="comment">// 此时，名字为car的bean，类型必须与方法的返回值类型一致</span></span><br><span class="line">    <span class="meta">@Lookup</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Car <span class="title">getCar</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> car;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 省略其他setter和getter，以及toString方法</span></span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@Scope</span>(<span class="string">"prototype"</span>)	<span class="comment">// 声明为多例</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> speed;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span> price;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 省略setter和getter，以及toString方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;可以看到，通过注解配置方法注入要简单的多，只需要通过一个<code>@Lookup</code>注解即可实现。测试方法与之前类似，结果也一样，我就不贴出来了。</p>
<br>

<p><strong>（三）为抽象方法使用方法注入</strong></p>
<p>&emsp;&emsp;实际上，方法注入还可以应用于抽象方法。既然方法注入的目的是替换原来的方法，那么原来的方法是否有实现，也就不重要了。所以方法注入也能用在抽象方法上面。但是有人可能会想一个问题：抽象方法只能在抽象类中，那这个类被定义为抽象类了，<code>Spring</code>容器如何为它创建对象呢？我们之前说过，使用了方法注入的类，<code>Spring</code>会使用<code>CGLib</code>生成它的一个代理类（子类），<code>Spring</code>创建的是这个代理类的对象，而不会去创建源类的对象，所以它是不是抽象的并不影响工作。如果配置了方法注入的类是一个抽象类，则方法注入机制的实现，就是去实现它的抽象方法。我们将<code>User</code>类改为抽象，如下所示：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 就算为抽象类使用了@Component，Spring容器在创建bean时也会跳过它</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line">    <span class="keyword">private</span> Car car;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 将getCar声明为抽象方法，它将会被代理类实现</span></span><br><span class="line">    <span class="meta">@Lookup</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> Car <span class="title">getCar</span><span class="params">()</span></span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 省略其他setter和getter，以及toString方法</span></span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;以上方式，方法注入仍然可以工作。</p>
<br>

<p><strong>（四）final方法和private方法无法使用方法注入</strong></p>
<p>&emsp;&emsp;<code>CGLib</code>实现动态代理的方法是创建一个子类，然后重写父类的方法，从而实现代理。但是我们知道，<code>final</code>方法和<code>private</code>方法是无法被子类重写的。这也就意味着，如果我们为一个<code>final</code>方法或者一个<code>private</code>方法配置了方法注入，那生成的代理对象中，这个方法还是原来那个，并没有被重写，比如像下面这样：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line">    <span class="keyword">private</span> Car car;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 方法声明为final，无法被覆盖，代理类中的getCar还是和下面一样</span></span><br><span class="line">    <span class="meta">@Lookup</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> Car <span class="title">getCar</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> car;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 省略其他setter和getter，以及toString方法</span></span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;我们依旧使用下面的测试方法，但是，在调用<code>c1.hashCode</code>方法时，抛出了空指针异常。说明<code>getCar</code>方法并没有被覆盖，还是直接返回了<code>car</code>这个成员变量。但是由于我们并没有为<code>user</code>注入<code>car</code>，所以<code>car == null</code>。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testConfig</span><span class="params">()</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">    AnnotationConfigApplicationContext context =</span><br><span class="line">        <span class="keyword">new</span> AnnotationConfigApplicationContext(AutoConfig<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line"></span><br><span class="line">    User user = context.getBean(User<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">    Car c1 = user.getCar();</span><br><span class="line">    Car c2 = user.getCar();</span><br><span class="line">    Car c3 = user.getCar();</span><br><span class="line">    <span class="comment">// 运行到这里，抛出空指针异常</span></span><br><span class="line">    System.out.println(c1.hashCode());</span><br><span class="line">    System.out.println(c2.hashCode());</span><br><span class="line">    System.out.println(c3.hashCode());</span><br><span class="line">    user.spCar();</span><br><span class="line">    user.spCar();</span><br><span class="line">    user.spCar();</span><br><span class="line">    System.out.println(user.getClass().getSuperclass());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<br>

<h1 id="三、总结"><a href="#三、总结" class="headerlink" title="三、总结"></a>三、总结</h1><p>&emsp;&emsp;以上大致介绍了一下方法注入的作用，实现原理，以及重点介绍了一下查找方法注入的使用。查找方法注入可以将我们的一个方法，覆盖成为一个去<code>Spring</code>容器中查找特定<code>bean</code>的方法，从而解决单例<code>bean</code>无法依赖多例<code>bean</code>的问题。其实，方法注入能够注入任何方法，而不仅仅是查找方法，但是由于任何方法注入使用的不多，所以这篇博客就不提了，感兴趣的可以自己去<code>Spring</code>文档中了解。最后，若以上描述存在错误或不足，欢迎指正，共同进步。</p>
<br>

<h1 id="四、参考"><a href="#四、参考" class="headerlink" title="四、参考"></a>四、参考</h1><ul>
<li><a href="https://www.docs4dev.com/docs/zh/spring-framework/4.3.21.RELEASE/reference/beans.html#beans-factory-method-injection" target="_blank" rel="noopener">Spring-4.3.21官方文档——方法注入</a></li>
</ul>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-05-12T18:25:01.697Z" itemprop="dateUpdated">2020-05-13 02:25:01</time>
</span><br>


        
        世界是个球，前方总有路！
        
    </div>
    
    <footer>
        <a href="http://tewuyiang.gitee.io/blog">
            <img src="/blog/img/avatar.jpg" alt="特务依昂">
            特务依昂
        </a>
    </footer>
</blockquote>

        


        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/Spring/" rel="tag">Spring</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://tewuyiang.gitee.io/blog/Spring%E6%96%B9%E6%B3%95%E6%B3%A8%E5%85%A5%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/&title=《Spring方法注入的使用与实现原理》 — 博客主页&pic=http://tewuyiang.gitee.io/blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://tewuyiang.gitee.io/blog/Spring%E6%96%B9%E6%B3%95%E6%B3%A8%E5%85%A5%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/&title=《Spring方法注入的使用与实现原理》 — 博客主页&source=一个未来程序员的博客~~~" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://tewuyiang.gitee.io/blog/Spring%E6%96%B9%E6%B3%95%E6%B3%A8%E5%85%A5%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Spring方法注入的使用与实现原理》 — 博客主页&url=http://tewuyiang.gitee.io/blog/Spring%E6%96%B9%E6%B3%95%E6%B3%A8%E5%85%A5%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/&via=http://tewuyiang.gitee.io/blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://tewuyiang.gitee.io/blog/Spring%E6%96%B9%E6%B3%95%E6%B3%A8%E5%85%A5%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C%E2%80%94%E2%80%94%E5%A6%82%E4%BD%95%E4%BF%9D%E8%AF%81%E7%BD%91%E7%BB%9C%E4%BC%A0%E8%BE%93%E7%9A%84%E5%AE%89%E5%85%A8%E6%80%A7/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">计算机网络——如何保证网络传输的安全性</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/%E6%B5%85%E6%9E%90Spring%E4%B8%ADAOP%E7%9A%84%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86%E2%80%94%E2%80%94%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">浅析Spring中AOP的实现原理——动态代理</h4>
      </a>
    </div>
  
</nav>



    




















</article>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/blog/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>特务依昂 &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://tewuyiang.gitee.io/blog/Spring%E6%96%B9%E6%B3%95%E6%B3%A8%E5%85%A5%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/&title=《Spring方法注入的使用与实现原理》 — 博客主页&pic=http://tewuyiang.gitee.io/blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://tewuyiang.gitee.io/blog/Spring%E6%96%B9%E6%B3%95%E6%B3%A8%E5%85%A5%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/&title=《Spring方法注入的使用与实现原理》 — 博客主页&source=一个未来程序员的博客~~~" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://tewuyiang.gitee.io/blog/Spring%E6%96%B9%E6%B3%95%E6%B3%A8%E5%85%A5%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Spring方法注入的使用与实现原理》 — 博客主页&url=http://tewuyiang.gitee.io/blog/Spring%E6%96%B9%E6%B3%95%E6%B3%A8%E5%85%A5%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/&via=http://tewuyiang.gitee.io/blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://tewuyiang.gitee.io/blog/Spring%E6%96%B9%E6%B3%95%E6%B3%A8%E5%85%A5%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: false };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '人呢，怎么不见了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!欢迎回来!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
